package dao.daoimpl;

import util.DataBase;
import dao.CollectionDao;
import bean.Collection;
import bean.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class CollectionDaoImpl implements CollectionDao {
    @Override
    public ArrayList<Collection> getCollections(User user) throws SQLException {
        ArrayList<Collection> collections=new ArrayList<>();
        String sql="select * from collection_table where collection_user='"+user.getAccount()+"'";
        DataBase.init();
        ResultSet resultSet=DataBase.inquire(sql);
        while (resultSet.next()){
            int id=resultSet.getInt("collection_id");
            int articleId=resultSet.getInt("collection_articleId");
            String userId=resultSet.getString("collection_user");
            collections.add(new Collection(id,userId,articleId));
        }
        DataBase.release();
        return collections;
    }

    @Override
    public boolean deleteCollection(int id) {
        String sql="delete from collection_table where collection_id='"+id+"'";
        DataBase.init();
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean addCollection(Collection collection) {
        String sql="insert into collection_table (collection_articleId,collection_user) values " +
                "('"+collection.getArticleId()+"','"+collection.getUser()+"')";
        DataBase.init();
        int flag=DataBase.operate(sql);
        DataBase.release();
        if(flag>0){
            return true;
        }
        return false;
    }
}
